﻿using Microsoft.Xna.Framework;
using System;

namespace BattleFury.Lib.Time
{
	public class WorldTimeColor
	{
		#region Variables

		public BattleFuryWorldTime WorldTime { get; set; }

		public byte R { get { return GetLightValue("R"); } }
		public byte G { get { return GetLightValue("G"); } }
		public byte B { get { return GetLightValue("B"); } }
		public byte A { get { return GetLightValue("A"); } }
		public Color Color { get { return new Color(R, G, B, A); } }

		#endregion

		#region Constructor

		public WorldTimeColor(BattleFuryWorldTime worldTime)
		{
			WorldTime = worldTime;
		}

		#endregion

		#region Methods

		public byte GetLightValue(string channel)
		{
			var midnightLightValue = 0;
			var sunriseLightValue = 0;
			var noonLightValue = 0;
			var sunsetLightValue = 0;
			var hour = WorldTime.Hour;
			var minute = WorldTime.Minute;
			var minutesInHour = WorldTime.MinutesInHour;
			var hoursInDay = WorldTime.HoursInDay;
			var midnight = WorldTime.Midnight;
			var sunrise = WorldTime.Sunrise;
			var noon = WorldTime.Noon;
			var sunset = WorldTime.Sunset;

			switch (channel.ToLower())
			{
				case "r":
					midnightLightValue = WorldTime.MidnightColor.R;
					sunriseLightValue = WorldTime.SunriseColor.R;
					noonLightValue = WorldTime.NoonColor.R;
					sunsetLightValue = WorldTime.SunsetColor.R;
					break;
				case "g":
					midnightLightValue = WorldTime.MidnightColor.G;
					sunriseLightValue = WorldTime.SunriseColor.G;
					noonLightValue = WorldTime.NoonColor.G;
					sunsetLightValue = WorldTime.SunsetColor.G;
					break;
				case "b":
					midnightLightValue = WorldTime.MidnightColor.B;
					sunriseLightValue = WorldTime.SunriseColor.B;
					noonLightValue = WorldTime.NoonColor.B;
					sunsetLightValue = WorldTime.SunsetColor.B;
					break;
				case "a":
					midnightLightValue = WorldTime.MidnightColor.A;
					sunriseLightValue = WorldTime.SunriseColor.A;
					noonLightValue = WorldTime.NoonColor.A;
					sunsetLightValue = WorldTime.SunsetColor.A;
					break;
			}

			float ret = midnightLightValue;

			if (hour >= midnight && hour < sunrise)
			{
				var diff = (float)((sunrise * minutesInHour) - (midnight * minutesInHour));
				var tick = ((hour * minutesInHour) + minute) / diff;
				var lightDiff = (sunriseLightValue - midnightLightValue);
				if (diff != 0f)
				{
					return Convert.ToByte(MathHelper.Clamp(midnightLightValue + (tick * lightDiff), midnightLightValue, sunriseLightValue));
				}
				else
				{
					return Convert.ToByte(midnightLightValue);
				}
			}

			if (hour >= sunrise && hour < noon)
			{
				var diff = (float)((noon * minutesInHour) - (sunrise * minutesInHour));
				var tick = (((hour * minutesInHour) + minute) - (sunrise * minutesInHour)) / diff;
				var lightDiff = (noonLightValue - sunriseLightValue);
				if (diff != 0f)
				{
					return Convert.ToByte(MathHelper.Clamp(sunriseLightValue + (tick * lightDiff), sunriseLightValue, noonLightValue));
				}
				else
				{
					return Convert.ToByte(sunriseLightValue);
				}
			}

			if (hour >= noon && hour < sunset)
			{
				var diff = (float)((sunset * minutesInHour) - (noon * minutesInHour));
				var tick = (((hour * minutesInHour) + minute) - (noon * minutesInHour)) / diff;
				var lightDiff = (sunsetLightValue - noonLightValue);
				if (diff != 0f)
				{
					return Convert.ToByte(MathHelper.Clamp(noonLightValue + (tick * lightDiff), sunsetLightValue, noonLightValue));
				}
				else
				{
					return Convert.ToByte(noonLightValue);
				}
			}

			if (hour >= sunset && hour < (hoursInDay - sunset) + sunset)
			{
				var diff = (float)((((hoursInDay - sunset) + sunset) * minutesInHour) - (sunset * minutesInHour));
				var tick = (((hour * minutesInHour) + minute) - (sunset * minutesInHour)) / diff;
				var lightDiff = (sunsetLightValue - midnightLightValue);
				if (diff != 0f)
				{
					return Convert.ToByte(MathHelper.Clamp(sunsetLightValue - (tick * lightDiff), midnightLightValue, sunsetLightValue));
				}
				else
				{
					return Convert.ToByte(sunsetLightValue);
				}
			}

			return Convert.ToByte(ret);
		}



		#endregion
	}
}